<?php
namespace app\admin\controller;

class AdminLog extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'AdminLog';
    }
    //
    protected function _before_index(){
        $thead = [
            'alid' => 'ALID',
            'aid' => '登录账号',
            'ip' => 'IP地址',
            'token' => 'token',
            'agent' => '登录终端',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
    }
    //
    protected function _filter($map){
        foreach($map as $key => $row){
            if($row[0] == 'aid'){
                $aids = model('Admin')->where('username', 'like', $row[2])->column('aid');
                $map[$key] = ['aid', 'in', $aids];
            }
        }
        return $map;
    }
    //
    protected function _after_list($list){
        $adminArr = model('Admin')->column('username', 'aid');
        foreach($list as $key => $row){
            $list[$key]['aid'] = $adminArr[$row['aid']] ?? '';
            $list[$key]['agent'] = agent($row['agent']);
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){}
    //
    protected function _before_insert($data){
        return $data;
    }
    //
    protected function _after_add($data, $id){}
    //
    protected function _before_edit(){}
    //
    protected function _filter_edit($list){
        return $list;
    }
    //
    protected function _before_save($data){
        return $data;
    }
    //
    protected function _after_edit($data){}
    //
    protected function _filter_del(){
        return [];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'alid', 'value' => $row['alid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}